/*
    This source file is part of GeNa project.
    Copyright © GeNa project 2008
    For the latest info, see http://code.google.com/p/genaproject/ or  http://lumpyproject.forums-rpg.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef EDITOR_WINDOW_H_INCLUDED
#define EDITOR_WINDOW_H_INCLUDED


#include "gl_window.h"
#include "managed_window.h"
#include "free_camera.h"
#include "property_manager.h"
#include "math/vector.h"

#include <boost/lambda/lambda.hpp>
using namespace boost::lambda;

class EditorWindow : public GLWindow, public ManagedWindow
{
private:
    enum
    {
        idMenuModel
    };


    math::vector3q m_center;
    math::vector3q m_3dCenter;
    float m_zoom;
    int m_res;
    int m_res2;
    int m_view; //0-xy 1-xz 2-yz

    class Axis*  m_axis;

    wxMenu* popupMenu;
    int m_mouseX, m_mouseY;

    math::matrix4f m_viewMat;
    math::matrix4f m_projMat;
public:
    template<class Archive> EditorWindow(wxWindow* parent, Archive* ar) : GLWindow(parent, wxDefaultPosition, wxDefaultSize)
    {
        propMgrInstance.addProperty(this, "EditorWindow", "res", 4, false, &EditorWindow::m_res, &EditorWindow::updateGridPropTable, (_1 >=1) && (_1 <=0xffffff));
        propMgrInstance.addProperty(this, "EditorWindow", "res2", 64, false, &EditorWindow::m_res2, &EditorWindow::updateGridPropTable, (_1 >=1) && (_1 <=0xffffff));

        m_zoom = 10;
        m_center.x = 0; m_center.y = 0;
        m_3dCenter.x = 0; m_3dCenter.y = 0; m_3dCenter.z = 0;
        m_view = 0;

        popupMenu = new wxMenu(_T(""));
        popupMenu->Append(idMenuModel, _("&Modèle …"), _("Ajouter un nouveau modèle …"));
    }
    void init();

    ~EditorWindow()
    {
        propMgrInstance.removeObj(this);
    }

    template<class Archive> static ManagedWindow* createWindow(wxWindow* parent, Archive* ar)
    {
        EditorWindow* wnd = new EditorWindow(parent, ar);
        return wnd;
    }

    ManagedWindow* clone(wxWindow* parent)
    {
        return createWindow<void>(parent, NULL);
    }

    wxWindow* getWindow()
    {
        return this;
    }

    std::string getName()
    {
        return "EditorWindow";
    }

    template<class Archive> void saveWindow( Archive* ar)
    {
    }

    void createGrid();
    void updateGrid();
    void updateView();

    math::vector3q getPosition(int x, int y);
    math::vector3q getAlignedPosition(int x, int y);
    math::vector3q getVector(int x, int y);
    math::vector3q getAlignedVector( int x, int y);
    math::vector3q getAligned( math::vector3q p);
    math::vector3q to3dVector(math::vector3q v);

    void onMouseEvent(wxMouseEvent &event);
    void onKeyDown(wxKeyEvent &event);
    void onMenuEvent(wxCommandEvent &event);

    virtual void update(int interval);

    void copy();
    void paste();
private:
    void updateGridPropTable(int)
    {
        if( isInitialized)
            updateGrid();
    }


    DECLARE_EVENT_TABLE()

};

#endif // EDITOR_WINDOW_H_INCLUDED
